package com.workpaper.app.utils.resource;
/**
 * 路径工具
 * @author zyj
 *
 */

import java.io.IOException;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;

import com.workpaper.AppEnvironment;
import com.workpaper.app.consts.AppConsts;

public final class PathUtils {
	/**
	 * 获取项目资源对应的URL信息
	 * @param relatePath， 资源相对路径， ""表示根路径
	 * @return
	 */
	public static URL getPathURL(String relatePath) {
		try {
			return FileLocator.toFileURL(Platform.getBundle(AppConsts.APP_ID).getEntry(relatePath));
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取应用类路径（classes）的绝对路径
	 * @return
	 */
	public static String getAppClassesPath() {
		String appPath = getAppRootPath();
		String classPath = appPath + (AppEnvironment.IS_DEV ? "bin/" : "");
		return classPath;
	}
	
	/**
	 * 获取应用根路径的绝对路径
	 * @return
	 */
	public static String getAppRootPath() {
		URL rootPathURL = getAppRootPathURL();
		if(rootPathURL == null) {
			return null;
		}
		String absPath = rootPathURL.getPath();
		if(Platform.OS_WIN32.equals(Platform.getOS())) {
			return absPath.substring(1);	//windows系统，移除开头第一个/
		}
		return absPath;
	}
	/**
	 * 获取应用根路径对应的URL
	 * @return
	 */
	private static URL getAppRootPathURL() {
		return getPathURL("");
	}
}
